package com.middleware.mvc.listener;

import com.middleware.mvc.loaders.AppPropertiesLoader;
import com.middleware.mvc.loaders.WebXmlServletLoader;
import com.middleware.spring.context.ApplicationContext;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import java.util.Map;

@WebListener
public class ContextLoaderListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent sce) {

        String parentPath = AppPropertiesLoader.getProperty("simple.spring.scan");
        ApplicationContext applicationContext = new ApplicationContext(parentPath);
        ServletContext servletContext = sce.getServletContext();
        servletContext.setAttribute("applicationContext", applicationContext);

        Map<String, String> stringStringMap = WebXmlServletLoader.parseWebXmlServletMappings(servletContext);
        servletContext.setAttribute("servletMappings", stringStringMap);
        WebXmlServletLoader.setServletMappings(stringStringMap);
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        ServletContextListener.super.contextDestroyed(sce);
    }
}
